package com.supreme.businessserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.businessserver.service.ProductQualityService;
import com.supreme.commonentity.mysql.primary.BizProduct;
import com.supreme.commonentity.mysql.primary.BizQuality;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "product-quality")
public class ProductQualityController {

    private final ProductQualityService productQualityService;

    @Autowired
    public ProductQualityController(ProductQualityService productQualityService) {
        this.productQualityService = productQualityService;
    }

    @GetMapping(value = "productList")
    public AjaxResp productList() {
        List<BizProduct> productList = this.productQualityService.getProductList();
        return new AjaxResp(productList);
    }

    @GetMapping(value = "qualityList")
    public AjaxResp qualityList() {
        List<BizQuality> qualityList = this.productQualityService.getQualityList();
        return new AjaxResp(qualityList);
    }

    @PostMapping(value = "createPQ")
    public AjaxResp createProductQuality(@RequestBody JSONObject postData) {
        String productCode = postData.getString("productCode");
        String qualityCode = postData.getString("qualityCode");
        Integer domainType = postData.getInteger("domainType");
        Integer status = postData.getInteger("status");

        BizProduct bizProduct = new BizProduct() {{
            setProductCode(productCode);
            setDomainType(domainType);
        }};
        BizQuality bizQuality = new BizQuality() {{
            setQualityCode(qualityCode);
            setDomainType(domainType);
        }};

        this.productQualityService.createProductQuality(bizProduct, bizQuality, status);

        return new AjaxResp();
    }
}
